﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Payton.WP7.Common.Interface;
using Hammock;
using Payton.WP7.Common.Model;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
using Hammock.Authentication.OAuth;
using Hammock.Web;
using Utility;
using WeiBoSdk;

namespace Payton.WP7.Common.Renren
{
    public class RenrenUserService : UserServiceBase
    {
        //string _fliedList = "";
        protected override void Initialize()
        {
            GET_CURRENT_USER = "/restserver.do";
            GET_USER = "/users/show.json";
            FOLLOW_USER = "";
            UNFOLLOW_USER = "";
            FRIENDSHIP = "";
            GET_USER_LIST = "/api/user/infos?format=json";
            GET_FANS = "/api/friends/fanlist?format=json";
            GET_FANS_LITE = "/api/friends/fanlist_s?format=json";
            GET_IDOLS = "/api/friends/idollist?format=json";
            GET_IDOLS_LITE = "/api/friends/idollist_s?format=json";
        }

        protected override RestClient CreateRestClient()
        {
            return new RestClient
            {
                Authority = "http://api.renren.com",
                HasElevatedPermissions = true,
                QueryHandling = QueryHandling.AppendToParameters
            };
            //return base.CreateRestClient();
        }

        protected override RestRequest CreateRestRequest(SNSRequest snsRequest)
        {
            var req = base.CreateRestRequest(snsRequest);
            req.Method = WebMethod.Post;

            return req;
        }

        protected override string[] GetParamtersList(Model.SNSType type)
        {
            string[] list;
            switch (type)
            {
                case SNSType.SingleUser:
                    list = new string[] { 
                        SNSParametersConstants.METHOD,
                        SNSParametersConstants.V,
                        SNSParametersConstants.FORMAT,
                        SNSParametersConstants.UIDS,
                        SNSParametersConstants.FIELDS
                    };
                    break;
                case SNSType.Friends:
                    list = new string[] { 
                        SNSParametersConstants.USER_ID,
                        SNSParametersConstants.CURSOR ,
                        SNSParametersConstants.COUNT
                    };
                    break;
                case SNSType.Followers:
                    list = new string[] { 
                        SNSParametersConstants.USER_ID,
                        SNSParametersConstants.CURSOR ,
                        SNSParametersConstants.COUNT
                    };
                    break;
                case SNSType.HotPeople:
                    return new string[]
                    { 
                        
                    };

                case SNSType.SuggestionPeoples:
                    list = new string[] { 
                        //SNSParametersConstants.COUNT,
                        //SNSParametersConstants.BASE_APP
                    };
                    break;
                default:
                    list = new string[] { };
                    break;
            }
            return list;
        }

        protected override System.Collections.Generic.IList<Model.SNSUser> Mapping(string res, SNSResult result)
        {
            JObject o = JObject.Parse("{\"users\":" + res + "}");


            List<Model.SNSUser> users = o.SelectToken("users").Select(
                s => factory.GetMapping<JToken, Model.SNSUser>().Map(s)
                ).ToList();
            return users;
        }

        protected override SNSUser MappingSingle(string res, SNSResult result)
        {
            JObject o = JObject.Parse(res.Trim('[', ']'));

            return factory.GetMapping<JToken, Model.SNSUser>().Map(o);
        }

        public override void GetUser(SNSRequest snsRequest, Action<SNSUser, SNSResult> action)
        {
            //snsRequest.Parameters.Add(new SNSParameter(SNSParametersConstants.METHOD, "users.getInfo") { Style = ParameterStyle.Normal });
            //snsRequest.Parameters.Add(new SNSParameter(SNSParametersConstants.V, "1.0") { Style = ParameterStyle.Normal });
            //snsRequest.Parameters.Add(new SNSParameter(SNSParametersConstants.FORMAT, "JSON") { Style = ParameterStyle.Normal });

            //base.GetUser(snsRequest, action);
            var list = new List<Parameter>();
            list.Add(new Parameter("access_token", Account.AccessToken));
            list.Add(new Parameter("method", "users.getInfo"));
            list.Add(new Parameter("v", "1.0"));
            list.Add(new Parameter("format", "json"));
            var post = HttpRequestFactory.CreateHttpRequest(Method.POST) as HttpPost;
            int num = 0;
            post.AppSecret = Provider.AuthSettings.CONSUMER_KEY_SECRET;
            post.Request("http://api.renren.com/restserver.do", list, new AsyncRequestCallback((c, str) =>
            {
                JObject o = JObject.Parse(str.Trim('[', ']'));
                var user =  factory.GetMapping<JToken, Model.SNSUser>().Map(o);

                if (action != null)
                {
                    action(user, new SNSResult() { IsSuccess = true, Message ="ok" });
                }
            }), out num);
        }


    }
}
